package com.hl.order.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hl.order.domain.Goods;
import com.hl.order.service.IGoodsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 商品列表Controller
 * 
 * @author ruoyi
 * @date 2025-08-08
 */
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询商品列表列表
     */
//    @RequiresPermissions("order:goods:list")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods)
    {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    @GetMapping("/selectGoodsByShopId")
    public List<Goods> selectGoodsByShopId(Integer shopId){
        return goodsService.selectGoodsByShopId(shopId);
    }

    /**
     * 导出商品列表列表
     */
    @RequiresPermissions("order:goods:export")
    @Log(title = "商品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods)
    {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "商品列表数据");
    }

    /**
     * 获取商品列表详细信息
     */
    @RequiresPermissions("order:goods:query")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Integer goodsId)
    {
        return success(goodsService.selectGoodsByGoodsId(goodsId));
    }

    /**
     * 新增商品列表
     */
    @RequiresPermissions("order:goods:add")
    @Log(title = "商品列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods)
    {
        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改商品列表
     */
    @RequiresPermissions("order:goods:edit")
    @Log(title = "商品列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods)
    {
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除商品列表
     */
    @RequiresPermissions("order:goods:remove")
    @Log(title = "商品列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Integer[] goodsIds)
    {
        return toAjax(goodsService.deleteGoodsByGoodsIds(goodsIds));
    }

    /**
     * 根据店铺ID查询已上架商品数量
     */
//    @RequiresPermissions("order:goods:query")
    @GetMapping("/onlineCount/{shopId}")
    public AjaxResult getOnlineGoodsCount(@PathVariable("shopId") Integer shopId)
    {
        try {
            int count = goodsService.countOnlineGoodsByShopId(shopId);
            return AjaxResult.success("查询成功", count);
        } catch (Exception e) {
            logger.error("查询已上架商品数量失败", e);
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据店铺ID查询已下架商品数量
     */
//    @RequiresPermissions("order:goods:query")
    @GetMapping("/offlineCount/{shopId}")
    public AjaxResult getOfflineGoodsCount(@PathVariable("shopId") Integer shopId)
    {
        try {
            int count = goodsService.countOfflineGoodsByShopId(shopId);
            return AjaxResult.success("查询成功", count);
        } catch (Exception e) {
            logger.error("查询已下架商品数量失败", e);
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据店铺ID查询商品总数
     */
//    @RequiresPermissions("order:goods:query")
    @GetMapping("/totalCount/{shopId}")
    public AjaxResult getTotalGoodsCount(@PathVariable("shopId") Integer shopId)
    {
        try {
            int count = goodsService.countTotalGoodsByShopId(shopId);
            return AjaxResult.success("查询成功", count);
        } catch (Exception e) {
            logger.error("查询商品总数失败", e);
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据店铺ID查询库存售罄商品数量
     */
//    @RequiresPermissions("order:goods:query")
    @GetMapping("/outOfStockCount/{shopId}")
    public AjaxResult getOutOfStockGoodsCount(@PathVariable("shopId") Integer shopId)
    {
        try {
            int count = goodsService.countOutOfStockGoodsByShopId(shopId);
            return AjaxResult.success("查询成功", count);
        } catch (Exception e) {
            logger.error("查询库存售罄商品数量失败", e);
            return AjaxResult.error("查询失败：" + e.getMessage());
        }
    }
}
